<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-16 14:10:44
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-08 09:46:43
 -->
<template>
  <div class="import-container">
    <el-button size="mini" class="filter-item" type="primary" @click="to">新增部门</el-button>
    <el-dialog ref="importForm" :visible.sync="dialogShow" title="导入部门信息">
      <el-tree
        ref="tree"
        :data="data"
        :props="defaultProps"
        show-checkbox
        node-key="wwx_id"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialogShow: false,
      defaultProps: {
        label: 'name',
        children: 'children',
        disabled: 'disabled'
      },
      data: []
    }
  },
  methods: {
    to() {
      this.dialogShow = true
      this.$api.enterprise.department
        .getDeptList()
        .then(res => {
          this.data = res.data
        }).catch(err => {
          // this.dialogShow = false
          console.log(err)
        })
    },
    cancel() {
      this.dialogShow = false
    },
    doSubmit() {
      // console.log(this.$refs.tree.getCheckedNodes())
      // return
      this.$api.enterprise.department
        .saveDepartment({
          data: { 
            wwx_dept_list: this.$refs.tree.getCheckedNodes()
          }
        })
        .then(res => {
          this.dialogShow = false
          this.sup_this.init()
        }).catch(err => {
          console.log(err)
        })
    },
    loadNode(node, resolve) {
      // if (node.level === 0) {
      //   this.$api.enterprise.enterprise
      //     .getDeptList(this.enterpriseId)
      //     .then(res => {
      //       this.dialogShow = true
      //       resolve(res.data)
      //     }).catch(err => {
      //       this.dialogShow = false
      //       console.log(err)
      //     })
      // }
      
      // if (node.level === 1) {
      //   this.$api.enterprise.enterprise
      //     .getDeptUserSimpleList({
      //       params: { enterprise_id: this.enterpriseId, dept_id: node.data.id}
      //     })
      //     .then(res => {
      //       this.dialogShow = true
      //       resolve(res.data)
      //     }).catch(err => {
      //       this.dialogShow = false
      //       console.log(err)
      //     })
      // }
      // if (node.level > 1) return resolve([])

      // setTimeout(() => {
      //   const data = [{
      //     name: 'leaf',
      //     leaf: true
      //   }, {
      //     name: 'zone'
      //   }];

      //   resolve(data);
      // }, 500);
    }
  }
}
</script>

<style scoped>
  .import-container {
    display: inline-block; margin: 0px 2px;
  }
</style>
